﻿using UnityEngine;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

// lazily-loaded, thread safe.
public class Singleton<T>
    where T : new()
{
    private static T s_instance = default(T);
    private static readonly System.Object padlock = new System.Object();

    //static constructors in C# are specified to execute only when an instance of 
    //the class is created or a static member is referenced, and to execute only once per AppDomain.
    static Singleton() 
    {
    }

    public static T NewInstance()
    {
        lock (padlock)
        {
            if (s_instance == null)
            {
                s_instance = new T();
            }
            return s_instance;
        }
    }

    /*public static T instance
    {
        get { return s_instance; }
    }*/

	public static T Instance
	{
		get
		{
			return GetInstance ();
		}
	}
	
	public static T GetInstance()
	{
		return NewInstance();
	}
}
